import 'package:cached_network_image/cached_network_image.dart';
import 'package:family_expense/features/account_user/widgets/account_group_user_details.dart';
import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class ActionLogView extends StatelessWidget {
  const ActionLogView(this.actionLog, {super.key});

  final ActionLogMixin actionLog;

  @override
  Widget build(BuildContext context) {
    // https://stackoverflow.com/questions/49388281/flutter-vertical-divider
    // Note: If you are using VerticalDivider as separator in Row widget then wrap Row with IntrinsicHeight ,
    // Container or SizedBox else VerticalDivider will not show up. For Container and SizedBox widget you need define height.
    return SizedBox(
      height: _getContainerHeight(),
      child: Row(
        mainAxisAlignment: actionLog.lastUpdated == actionLog.dateCreated
            ? MainAxisAlignment.start
            : MainAxisAlignment.spaceAround,
        children: [
          if (actionLog.lastUpdated != actionLog.dateCreated)
            _buildItem(
              context,
              Icon(MdiIcons.update, color: Colors.grey, size: 13.0),
              actionLog.updatedBy,
              actionLog.lastUpdated,
            ),
          if (actionLog.lastUpdated != actionLog.dateCreated)
            const VerticalDivider(),
          if (actionLog.lastUpdated == actionLog.dateCreated)
            const SizedBox(width: 8.0),
          _buildItem(
            context,
            Icon(MdiIcons.plus, color: Colors.grey, size: 13.0),
            actionLog.createdBy,
            actionLog.dateCreated,
          ),
        ],
      ),
    );
  }

  Widget _buildItem(
    BuildContext context,
    Widget icon,
    User? user,
    DateTime? dateTime,
  ) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Expanded(
          child: Row(
            children: [
              if (user?.avatarUrl.isNotNullOrEmpty == true)
                GestureDetector(
                  child: ClipOval(
                    child: CachedNetworkImage(
                      width: 35.0,
                      height: 35.0,
                      imageUrl: user!.avatarUrl!,
                      fit: BoxFit.cover,
                    ),
                  ),
                  onTap: () {
                    if (user.id.isNotNullOrEmpty) {
                      NavigatorHelper.push<bool>(
                        context,
                        AccountGroupUserDetails(user.id!),
                      );
                    }
                  },
                ),
              const SizedBox(width: 5.0),
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  if (user?.fullName.isNotNullOrEmpty == true)
                    SizedBox(
                      width: 110.0,
                      child: Text(
                        user!.fullName!,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  Row(
                    children: [
                      Text(
                        dateTime.formatDate(DatePatterns.shortDateTime),
                        style: Theme.of(
                          context,
                        ).textTheme.bodySmall!.copyWith(fontSize: 10.0),
                      ),
                      const SizedBox(width: 2.0),
                      icon,
                    ],
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  double _getContainerHeight() {
    return actionLog.createdBy?.avatarUrl.isNullOrEmpty == true &&
            actionLog.createdBy?.fullName.isNullOrEmpty == true &&
            actionLog.updatedBy?.avatarUrl.isNullOrEmpty == true &&
            actionLog.updatedBy?.fullName.isNullOrEmpty == true
        ? 20.0
        : 40.0;
  }
}
